package com.sora.sora.security.authorise.controller;

import com.sora.sora.common.annotation.Auth;
import com.sora.sora.common.constant.ResultCode;
import com.sora.sora.common.model.vo.ResultVO;
import com.sora.sora.common.utils.ResponseResult;
import com.sora.sora.security.authorise.model.param.RoleParam;
import com.sora.sora.security.authorise.service.RoleService;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@Auth(id = 1500, name = "角色管理")
@Api(value = "角色管理", tags = "角色管理")
@RestController
@RequestMapping("role")
public class RoleController {

    @Resource
    RoleService roleService;

    @Resource
    ResponseResult responseResult;

    @Auth(id = 1, name = "创建角色")
    @ApiOperation(value = "创建角色")
    @ApiResponses({
            @ApiResponse(code = 200, message = "创建成功"),
            @ApiResponse(code = 1002, message = "创建失败")
    })
    @PostMapping
    public ResultVO addRole(@RequestBody @Validated(RoleParam.CreateRole.class) RoleParam param) {
        int role = roleService.createRole(param);
        return role > 0 ? responseResult.ok() : responseResult.faile();
    }


    @Auth(id = 2, name = "编辑角色")
    @ApiOperation(value = "编辑角色")
    @ApiResponses({
            @ApiResponse(code = 200, message = "更新成功"),
            @ApiResponse(code = 1002, message = "更新失败")
    })
    @PutMapping
    public ResultVO updateRole(@RequestBody @Validated(RoleParam.UpdateRole.class) RoleParam param) {
        int role = roleService.updateRole(param);
        return role > 0 ? responseResult.ok() : responseResult.faile();
    }


    @Auth(id = 3, name = "删除多个角色")
    @ApiOperation(value = "删除多个角色")
    @ApiImplicitParam(name = "roleIds", value = "要删除的角色id的数组集合", required = true, dataType = "Int", paramType = "query", allowMultiple = true)
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 1002, message = "删除失败,并传回删除失败的角色id")
    })
    @DeleteMapping("/multi")
    public ResultVO delMultipleRole(@RequestParam("roleIds") Integer[] roleIds) {
        if (roleIds.length <= 0) {
            return new ResultVO(ResultCode.VALIDATE_FAILED, "数组的长度需要大于等于0");
        }
        List<Integer> list = roleService.delRole(Arrays.asList(roleIds));
        return list.isEmpty() ? responseResult.ok() : responseResult.faile(list);
    }


    @Auth(id = 4, name = "删除单个角色")
    @ApiOperation(value = "删除单个角色")
    @ApiImplicitParam(name = "id", value = "要删除的角色id", required = true,  dataType = "int", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 1002, message = "删除失败")
    })
    @DeleteMapping
    public ResultVO delRole(@RequestParam("id") int id) {
        List<Integer> list = new ArrayList<>();
        list.add(id);
        List<Integer> result = roleService.delRole(list);
        return result.isEmpty() ? responseResult.ok() : responseResult.faile(result);
    }


    @Auth(id = 5, name = "展示所有角色")
    @ApiOperation(value = "展示所有角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "每页查询的字段个数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "第几页", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "返回结果如下"),
            @ApiResponse(code = 1003, message = "未知错误，请联系管理员")
    })
    @GetMapping
    public ResultVO list(@RequestParam("page") int page, @RequestParam("limit") int limit) {
        return responseResult.ok(roleService.listRoles(page, limit));
    }


    @ApiOperation(value = "通过用户id给用户授予角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uids", value = "用户id数组", required = true, dataType = "Int", paramType = "query", allowMultiple = true),
            @ApiImplicitParam(name = "roleId", value = "要授权的角色id", required = true, dataType = "Int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "授权成功"),
            @ApiResponse(code = 1002, message = "授权失败,并返回授权失败的用户id")
    })
    @PostMapping("/grant")
    public ResultVO grantRoleByUserId(@RequestParam("uids") Integer[] uids, @RequestParam("roleId") Integer roleId) {
        if (uids.length == 0 || roleId <= 0) {
            return new ResultVO(ResultCode.VALIDATE_FAILED, "请传入正确的参数");
        }
        List<Integer> list = roleService.grantUserRole(Arrays.asList(uids), roleId);
        return list.isEmpty() ? responseResult.ok() : responseResult.faile(list);
    }


    @ApiOperation(value = "通过用户id取消用户角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uids", value = "用户id数组", required = true, dataType = "Int", paramType = "query", allowMultiple = true),
            @ApiImplicitParam(name = "roleId", value = "要取消授权的角色id", required = true, dataType = "Int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "取消授权成功"),
            @ApiResponse(code = 1002, message = "取消授权失败，并返回授权失败的用户id")
    })
    @DeleteMapping("/grant")
    public ResultVO cancelGrantRoleByUserId(Integer[] uids, Integer roleId) {
        if (uids.length == 0 || roleId <= 0) {
            return new ResultVO(ResultCode.VALIDATE_FAILED, "请传入正确的参数");
        }
        List<Integer> list = roleService.cancleGrantUserRole(Arrays.asList(uids), roleId);
        return list.isEmpty() ? responseResult.ok() : responseResult.faile(list);
    }


    @ApiOperation("获得所有记录总数")
    @GetMapping("/count")
    public ResultVO getAllCount() {
        return responseResult.ok(roleService.getCount());
    }
}
